package cn.edu.zut.hhr.controller;

import cn.edu.zut.hhr.entity.UserInfo;
import cn.edu.zut.hhr.mapper.UserInfoMapper;
import cn.edu.zut.hhr.service.IUserInfoService;
import cn.edu.zut.hhr.utils.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author hhr
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/user-info")
public class UserInfoController {

    @Resource
    private IUserInfoService userInfoService;
    @Resource(name = "batchSqlSession")
    private SqlSession batchsqlSession;

    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/role/{uid}")
    public RestResponse<List<UserInfo>> insertRoles(@PathVariable("uid") Long uid,
                                             @RequestParam("roles") List<String> roles,
                                             HttpSession session){
        UserInfoMapper userInfoMapper = batchsqlSession.getMapper(UserInfoMapper.class);
        String currentUsername = (String)session.getAttribute("username");
        List<UserInfo> userInfos = new ArrayList<>();
        for(String role: roles){
            UserInfo userInfo = getUserInfo(uid,currentUsername,role);
            userInfos.add(userInfo);
            userInfoMapper.insert(userInfo);
        }
        batchsqlSession.commit();
        return RestResponse.success(userInfos);
    }

    @PreAuthorize("hasAnyAuthority('admin')")
    @DeleteMapping("/role/{uid}")
    public RestResponse<Boolean> deleteRoles(@PathVariable("uid") Long uid,
                                                   @RequestParam("roles") List<String> roles){
        UserInfoMapper userInfoMapper = batchsqlSession.getMapper(UserInfoMapper.class);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(UserInfo::getUid,uid)
                .eq(UserInfo::getKey,"role")
                .in(UserInfo::getValue,roles);
        userInfoMapper.delete(wrapper);
        batchsqlSession.commit();
        return RestResponse.success("取消权限成功",true);
    }

    @GetMapping("/role/{uid}")
    public RestResponse<List<UserInfo>> getRoles(@PathVariable("uid") Long uid){
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUid,uid);
        return RestResponse.success(userInfoService.list(queryWrapper));
    }

    private UserInfo getUserInfo(Long id, String currentUsername, String role){
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(id);
        userInfo.setCreateAuthor(currentUsername);
        userInfo.setUpdateAuthor(currentUsername);
        userInfo.setKey("role");
        userInfo.setValue(role);
        return userInfo;
    }

}
